/**
 * 
 */
package com.hw.hcit.feature;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import android.util.Log;
/**
 * @author gaoshengpan
 *
 */
public class EnvironmentLightTestItem extends JTestItem {
    private TextView currentvalue;
    private String strLightinfo;
    Sensor mLightSensor;
    private  SensorEventListener mLightSensorListener;
    private SensorManager mSensorManager;
    private boolean powerClicked = false;
    private String TAG = "EnvironmentLightTestItem";
    private Button mButton1,mButton2;
    
    private boolean minValue ;
    private boolean maxValue;
    private boolean flag;
    /**
     * @param cb
     */
    public EnvironmentLightTestItem(JCtrlCallback cb) {
        super(cb);
        // TODO Auto-generated constructor stub
    }

    /**
     * 
     */
    public EnvironmentLightTestItem() {
        // TODO Auto-generated constructor stub
        mLightSensorListener = new SensorListener();
    }

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        strLightinfo = getString(R.string.envlight_value);
        SensorManager sensormanager = (SensorManager) Config.GetActivity().getApplicationContext().getSystemService("sensor");
        mSensorManager = sensormanager;
        
        Sensor sensor = mSensorManager.getDefaultSensor(5);
        mLightSensor = sensor;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.environmetlight_test,
                container, false);
        currentvalue = (TextView) rootView.findViewById(R.id.textView2);
        mButton1 = (Button) rootView.findViewById(R.id.button1);
        mButton2 = (Button) rootView.findViewById(R.id.button2);
        mButton1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(Config.CURR_PROJECT.isAuto()){
                    if (!Config.ageingTested) {
                        rewriteOneByteOfNv2499(13, 'P');
                    } else {
                        rewriteOneByteOfNv2499(44, 'P');
                    }
                }
                 testFinish(true, 1, true);
                
            }
        });
        mButton2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(Config.CURR_PROJECT.isAuto()){
                    if (!Config.ageingTested) {
                        rewriteOneByteOfNv2499(13, 'F');
                    } else {
                        rewriteOneByteOfNv2499(44, 'F');
                    }
                }
                testFinish(false, 1, false);
                
            }
        });

        return rootView;
    }
    /*15-12-01 gaoshenpan fix it unregisterListener in OnPause more reasonable begin*/
    @Override
    public void onPause() {
       // TODO Auto-generated method stub
       super.onPause();
       mSensorManager.unregisterListener(mLightSensorListener);
    }
    /*15-12-01 gaoshenpan fix it unregisterListener in OnPause more reasonable end*/
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        /* 15-11-28 gaoshenpan fix add judgeValue for lightsebeor begin*/
        minValue =false;
        maxValue = false;
        mButton1.setEnabled(false);
        powerClicked = false;
        flag = mSensorManager.registerListener(mLightSensorListener,
                mLightSensor, 3);
        /* 15-11-28 gaoshenpan fix add judgeValue for lightsebeor end*/
        String s = (new StringBuilder()).append("bSucceed is ").append(flag)
                .toString();
        Log.d(TAG, s);
    }
    
    class SensorListener implements SensorEventListener

    {

        public void onAccuracyChanged(Sensor sensor, int i) {
        }

        public void onSensorChanged(SensorEvent sensorevent) {
            TextView textview = currentvalue;
            StringBuilder stringbuilder = new StringBuilder();
            String s = strLightinfo;
            StringBuilder stringbuilder1 = stringbuilder.append(s).append("\n");
            float f = sensorevent.values[0];
            Log.d(TAG, "onSensorChanged-->"+"values="+f);
           /* 15-11-28 gaoshenpan fix add judgeValue for lightsebeor begin*/
            if(f<=304 ){
                minValue = true;
                Log.d(TAG, "1");
            }
            if(f>=5006){
                maxValue = true;
                Log.d(TAG, "2");
            }
            if(flag && minValue && maxValue){
                Log.d(TAG, "3");
                mButton1.setEnabled(true);    
            }
            /* 15-11-28 gaoshenpan fix add judgeValue for lightsebeor end*/
            String s1 = stringbuilder1.append(f).toString();
            textview.setText(s1);
        }
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        return true;
    }
     /*@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
            dissmissPowerClicked();
            Log.d(TAG, " --------->KeyEvent.KEYCODE_VOLUME_DOWN");
            if (powerClicked) {
                Log.d(TAG,
                        "powerClicked --------->KeyEvent.KEYCODE_VOLUME_DOWN");
                testFinish(false, 1, false);
                return true;
            }
            
            if (tested) {
                Log.d(TAG,
                        "tested --------->KeyEvent.KEYCODE_VOLUME_DOWN");
                testFinish(true, 1, true);
            } else {
                Log.d(TAG, " --------->showTestUncomplete");
                showTestUncomplete();
            }
            
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
            dissmissPowerClicked();    
            testFinish(true, -1, true);
            
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
            
            Log.d(TAG, "powerClicked --------->KeyEvent.KEYCODE_POWER");
            powerClicked = true;
            showPowerClicked();
        }
        return true;
    }
*/
}
